package com.cloudeye.monitor.service;

import com.cloudeye.base.constant.Constants;
import com.cloudeye.monitor.listener.AppStatusChangeListener;
import org.I0Itec.zkclient.ZkClient;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 应用上下线状态监控
 */
@Service
public class AppStatusService {

    @Autowired
    private CuratorFramework curatorFramework;
    @Autowired
    private ZkClient zkClient;
    @Autowired
    private  AppInfoService appInfoService;

    public void init() throws Exception {
        //临时节点：/cloudeye/app/online 监控
        PathChildrenCache pathChildrenCache = new PathChildrenCache(curatorFramework, Constants.ROOT_PATH_EPHEMERAL, true);
        pathChildrenCache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
        pathChildrenCache.getListenable().addListener(new AppStatusChangeListener(this.zkClient,this.appInfoService));
//        pathChildrenCache.getListenable().addListener(new ScrollChildrenChangeListener(this.rabbitmqService, this.zkClient, this.appInfoService));
    }
}
